*** File takes FE risk and time estimates from Matlab. Used to plot FE distributions, show explanatory power of FE estimates (and demographic/socio-economic variables) in explaining moments of raw choice data, and  to calculate the exhibit on choice inconsistency.
* loads fixed effect estimates of preference and consistency parameters from fe_preference_estimation.m

clear
set more off
pause on

cd ""


foreach type in RP {
		clear
		insheet using fe_paras.csv, clear
		rename(v1-v7) (ind_id  `type'_th_t_base `type'_sig_th_t_base  `type'_K_t_base  `type'_r_t_base  `type'_sig_r_t_base  `type'_fval_t_base )
		sort ind_id
	}	



* Analysis which allows for the replication of Table 4 of the paper. Gets the median individual by risk aversion (discount rates) and gives the corresponding standard deviation of risk aversion (discount rates) as an average of +/- values around the individual. Combined with the indifference thresholds calculated in "full_model_estimation.m", they yield Table 4. 
* requires installing tsegen
*ssc install tsegen

sort RP_th_t_base
gen n_th=_n
tsset n_th


foreach i in 8 {
	* this generates the number as a mean of the past 8 observations. Since I want a window around an observation (above and below), the second step establishing the window around an observation is necessary
	tsegen RP_sig_th_t_base_l`i' = rowmean(L(0/`i').RP_sig_th_t_base)

	local wind=`i'/2
	gen RP_sig_th_t_base_w`wind'=F`wind'.RP_sig_th_t_base_l`i'
	}

*** Bounds on the coefficient of risk aversion for the median individual
foreach var of varlist  RP_th_t_base RP_sig_th_t_base_w* {
	capture drop pct_`var'
	gen pct_`var'=.

		replace pct_`var'=`var'[612] in 1
		
	}

br  pct_*   
	

	
	


foreach ut in _2s  {
	sort RP_r_t_base
	gen n_r`ut'=_n
	tsset n_r`ut'



	foreach i in 8 {
		* this generates the number as a mean of the past 8 observations. Since I want a window around an observation (above and below), the second step establishing the window around an observation is necessary
		tsegen RP_sig_r_t_base_l`i' = rowmean(L(0/`i').RP_sig_r_t_base)
		
		local wind=`i'/2
		gen RP_sig_r_t_base_w`wind'=F`wind'.RP_sig_r_t_base_l`i'
		}

	*** Bounds on the discount rate for the median individual
	foreach var of varlist RP_r_t_base RP_sig_r_t_base_w* {
		capture drop pct_`var'
		gen pct_`var'=.

			replace pct_`var'=`var'[612] in 1

			
		}
	}

local ut _2s
sort n_r`ut'
br  RP_r_t_base RP_sig_r_t_base pct*

*** adds in survey variables
merge 1:1 ind_id using survey_vars
drop _merge

*** adds in factor scores
merge 1:1 ind_id using factor_scores
drop _merge

save base_FE_para_all, replace
	
	
	
	